package com.wms.controller;

import com.wms.bean.PageBean;
import com.wms.bean.ResultBean;
import com.wms.core.R;
import com.wms.dto.AllocatedetailVo;
import com.wms.dto.AllocatetailQueryVo;
import com.wms.dto.AllocatetailSaveVo;
import com.wms.entity.Allocate;
import com.wms.entity.Allocatedetail;
import com.wms.entity.Suser;
import com.wms.service.AllocateService;
import com.wms.service.AllocatedetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
@RestController
@RequestMapping("/allocatetail")
@Api(tags = "调拨单详情管理--张开心")
@Slf4j
public class AllocatedetailController {

    @Autowired
    HttpServletRequest request;
    @Autowired
    private AllocatedetailService allocatedetailService;

    @PostMapping("/query")
    @ApiOperation(value = "条件分页查询调拨单详情")
    public ResultBean<PageBean<List<Allocatedetail>>> query(
            @ApiParam(value = "查询条件（含分页）", required = true)
            @RequestBody AllocatetailQueryVo queryVo
    ) {
        log.info("查询调拨单详情，条件：{}", queryVo);
        PageBean<List<Allocatedetail>> pageBean = allocatedetailService.query(queryVo);
        return pageBean.getList().size() > 0 ?
                R.success("查询成功", pageBean) :
                R.fail("查询失败");
    }

    @PostMapping("/save")
    @ApiOperation(value = "新增调拨单详情")
    public ResultBean<Integer> save(
            @ApiParam(value = "新增详情参数", required = true)
            @RequestBody AllocatetailSaveVo saveVo
    ) {
        log.info("新增调拨单详情：{}", saveVo);
        int rows = allocatedetailService.save(saveVo);
        return rows == 1 ?
                R.success("新增成功", rows) :
                R.fail("新增失败");
    }

    @PutMapping("/update")
    @ApiOperation(value = "更新调拨单详情")
    public ResultBean<Integer> update(
            @ApiParam(value = "更新详情参数", required = true)
            @RequestBody AllocatetailSaveVo saveVo
    ) {
        log.info("更新调拨单详情：{}", saveVo);
        int rows = allocatedetailService.update(saveVo);
        return rows == 1 ?
                R.success("更新成功", rows) :
                R.fail("更新失败");
    }

    @PutMapping("/remove/{id}")
    @ApiOperation(value = "逻辑删除调拨单详情")
    public ResultBean<Integer> remove(
            @ApiParam(value = "详情ID", required = true)
            @PathVariable Integer id
    ) {
        log.info("逻辑删除调拨单详情，ID：{}", id);
        int rows = allocatedetailService.remove(id);
        return rows == 1 ?
                R.success("删除成功", rows) :
                R.fail("删除失败");
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "物理删除调拨单详情")
    public ResultBean<Integer> delete(
            @ApiParam(value = "详情ID", required = true)
            @PathVariable Integer id
    ) {
        log.info("物理删除调拨单详情，ID：{}", id);
        int rows = allocatedetailService.removePhysics(id);
        return rows == 1 ?
                R.success("删除成功", rows) :
                R.fail("删除失败");
    }

    /**
     * 调拨详情图片上传文本上传
     * @param file
     * @return
     */
    @PostMapping(value="/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation(value = "拨单详情图片上传")
    public ResultBean upload(
            @RequestPart("file")
            @ApiParam(
                    value = "上传的文件（仅支持png和jsp）",
                    required = true,
                    type = "file"  // 关键：指定参数类型为文件
            )  MultipartFile file) {
       String url = allocatedetailService.toUpload(file,request);
        return url!=""?R.success("上传成功", url):R.fail("上传失败");
    }

    @PostMapping("/print")
    @ApiOperation(value = "打印调拨单详情（生成PDF）")
    public void printAllocatedetail(
            @ApiParam(value = "查询条件", required = true)
            @RequestBody AllocatetailQueryVo queryVo,
            HttpServletResponse response
    ) throws Exception {
        log.info("打印调拨单详情，查询条件：{}", queryVo);
        allocatedetailService.generatePrintPdf(queryVo, response);
    }



    @PostMapping("/exportExcel")
    @ApiOperation(value = "导出调拨单详情为Excel（直接下载）")
    public void exportExcel(
            @ApiParam(value = "查询条件", required = true)
            @RequestBody AllocatetailQueryVo queryVo,
            HttpServletResponse response
    ) throws IOException {
        log.info("导出调拨单详情Excel，条件：{}", queryVo);

        // 查询数据（不分页）
        queryVo.setPage(1);
        queryVo.setPageSize(Integer.MAX_VALUE);
        PageBean<List<Allocatedetail>> pageBean = allocatedetailService.query(queryVo);
        List<Allocatedetail> details = pageBean.getList();

        // 调用Service生成并下载Excel
        allocatedetailService.generateExcel(response, details);
    }
}
